package com.yanqu.road.server.manager.strongholdwar;

import com.yanqu.road.entity.activity.stronghold.*;
import com.yanqu.road.logic.bussiness.player.activity.CrossStrongholdBusiness;
import com.yanqu.road.logic.bussiness.player.activity.UserStrongholdBusiness;
import org.apache.commons.collections.ArrayStack;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StrongholdActivityModelGroup {
    private final Object lock = new Object();
    private int activityId;
    private Map<Integer, StrongholdActivityModel> groupMap = new ConcurrentHashMap<>();

    private List<StrongholdWildUserData> allWildUserDataList = new ArrayList<>();
    /**
     * 存储淘汰赛联盟积分
     */
    private Map<String, StrongholdEliminateUnionScore> unionScoreMap = new ConcurrentHashMap<>();

    /**
     * 玩家每回合的伤害值
     */
    private Map<Integer, Map<Long, CrossStrongholdUserRoungDamage>> userRoundDamageMap = new ConcurrentHashMap<>();
    private List<CrossStrongholdEliminateUnion> eliminateUnionList = new ArrayList<>();
    private List<CrossStrongholdEliminateDefendPatrons> defendPatronsList = new ArrayList<>();
    private List<CrossStrongholdEliminateDamage> eliminateDamageList = new ArrayList<>();

    public StrongholdActivityModelGroup(int activityId) {
        this.activityId = activityId;
        loadDbData();
    }

    public void loadDbData() {
        allWildUserDataList = UserStrongholdBusiness.getAllStrongholdWildUserData(activityId);
        unionScoreMap = CrossStrongholdBusiness.getStrongholdEliminateUnionScoreMap(activityId);
        userRoundDamageMap = CrossStrongholdBusiness.getCrossUserRoundDamage(activityId);
        eliminateUnionList = CrossStrongholdBusiness.getEliminateUnionList(activityId);
        defendPatronsList = CrossStrongholdBusiness.getEliminatePatronsList(activityId);
        eliminateDamageList = CrossStrongholdBusiness.getEliminateDamageRecordList(activityId);
    }

    public StrongholdActivityModel getActivityModel(int groupId) {
        if (!groupMap.containsKey(groupId)) {
            synchronized (lock) {
                if (!groupMap.containsKey(groupId)) {
                    StrongholdActivityModel activity = new StrongholdActivityModel(activityId, groupId);
                    activity.reload(allWildUserDataList, unionScoreMap, userRoundDamageMap, eliminateUnionList, defendPatronsList, eliminateDamageList);
                    groupMap.put(groupId, activity);
                }
            }
        }

        return groupMap.get(groupId);
    }

    public int getActivityId() {
        return activityId;
    }

    public List<StrongholdActivityModel> getActivityModelList() {
        return new ArrayList<>(groupMap.values());
    }
}
